#include "Engine/AuxTypesDX9.h"
#include "VertexDeclarationDX9.h"


CVertexDeclarationDX9::CVertexDeclarationDX9( )
{
    m_pVertexDeclaration9 = NULL;
}

CVertexDeclarationDX9::~CVertexDeclarationDX9( )
{
    SAFE_RELEASE(m_pVertexDeclaration9);
}

mVoid CVertexDeclarationDX9::Setup( )
{
    g_D3DDevice.pD3D9Device->SetVertexDeclaration(m_pVertexDeclaration9);
}

mBool CVertexDeclarationDX9::Create( const void *a_pShaderByteCode, size_t a_uByteCodeLength )
{
    if (m_pVertexDeclaration9)
        return false;

    HRESULT hr;
    D3DVERTEXELEMENT9 *pInputLayoutDesc;
    pInputLayoutDesc = new D3DVERTEXELEMENT9[m_Declaration.size() + 1];
    D3DVERTEXELEMENT9 Last = D3DDECL_END();
    pInputLayoutDesc[m_Declaration.size()] = Last;
    size_t i;
    size_t uOffset = 0;
    for (i = 0; i < m_Declaration.size(); i++)
    {
        pInputLayoutDesc[i].Usage                = DX9Semantic(m_Declaration[i].sSemanticName);
        pInputLayoutDesc[i].UsageIndex           = m_Declaration[i].uSemanticIndex;
        pInputLayoutDesc[i].Type                 = DX9DeclType(m_Declaration[i].VarType);
        pInputLayoutDesc[i].Stream               = m_Declaration[i].uInputSlot;
        pInputLayoutDesc[i].Offset               = uOffset;
        pInputLayoutDesc[i].Method               = D3DDECLMETHOD_DEFAULT;
        //pInputLayoutDesc[i].InstanceDataStepRate = 0;
        uOffset += g_uVarTypeSize[m_Declaration[i].VarType];
    }

    hr = g_D3DDevice.pD3D9Device->CreateVertexDeclaration(pInputLayoutDesc, &m_pVertexDeclaration9);

    delete [] pInputLayoutDesc;
    return (hr == S_OK);
}